#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import bs4
import requests
import urllib.request as ur

url = "http://photo.sina.com.cn/"

def load_href(url):
    # 下载urls
    response = requests.get(url)
    soup = bs4.BeautifulSoup(response.text, 'lxml')
    urls = []
    for a  in soup.find_all('a'):
        if a.has_attr('href'):
            href = a['href']
            if  href.startswith("http:") and href != url:
                urls.append(href)
    return urls

def load_image(url):
    # 根据url，下载资源
    response = requests.get(url)
    soup = bs4.BeautifulSoup(response.text, 'lxml')
    for k,a in enumerate(soup.find_all('img')):
        if a.has_attr('src'):
            image = a['src']
            try:
                print('loading', image)
                name = image.split('/')[-1] # 图片名
                if name.endswith('.jpg'):
                    ur.urlretrieve(image, 'images/'+name)
            except:
                pass

urls = load_href(url)
for url in urls:
    load_image(url)
